<template>
    <div class="myBook">
      <div v-for="(book,index) in books" :key="index">
        <van-card
            :num = "book.num"
            :price = "book.price"
            :desc="book.bookTitle"
            :title="book.title"
            :thumb="book.img"
        >
          <template #footer >
            <van-button size="small" @click="deleteBook(book)" type="danger">下架（删除）</van-button>
          </template>
        </van-card>
      </div>
    </div>
</template>

<script>
   import {ApiService} from "@/axios/apiService";
   import {Dialog} from "vant";
   const api = new ApiService("/userBook/getUserBookList")
   let userId = api.getCookie("userId")
   export default {
        props: {
        },
        data() {
          return {
            books:[],
          }
        },
        mounted() {
          console.log(userId)
          this.getUserBooks(userId)
        },
        methods:{
          getUserBooks(userId) {
            api.getParams({
              userId:userId
            }).then(res=> {
              this.books = res.data.data
            })
          },
          //删除
          deleteBook(book) {
            console.log(book)
            Dialog.alert({
              title:"系统提示",
              message:"确定删除图书'"+book.title+"'吗？",
            }).then(()=>{
              const bookApi = new ApiService("/userBook/delete")
              bookApi.getParams({
                id:book.id
              }).then(()=> {
                Dialog.alert({
                  message:"删除成功"
                }).then(()=>{
                  this.getUserBooks(userId)
                })
              })
            })
          }
        },
    };
</script>
